<?php

namespace Bundle\ConfigurationBundle;

use Symfony\Component\Yaml\Yaml;

class Configuration implements \Iterator, \Countable
{
    private $_configuration = array();
    
    public function __construct($config)
    {
        if(\is_string($config))
        {
            $ext = substr($config, -4);

            switch($ext)
            {
                case '.php' : $this->_configuration = $this->loadFromPHP($config);
                    break;
                case '.xml' : $this->_configuration = $this->loadFromXML($config);
                    break;
                case '.yml' : $this->_configuration = $this->loadFromYAML($config);
                    break;
                default:
                    throw new \Exception("Unknown config source: $config");
            }
        }
        else if(\is_array($config))
        {
            $this->_configuration = $config;
        }
        else
        {
            throw new \Exception('Smth unkcnol');
        }
    }

    public function merge($config)
    {
        if(\is_string($config))
        {
            $this->merge(new Configuration($config));
        }
        else if(\is_array($config))
        {
            $this->mergeRecursive($this->_configuration, $config);
        }
        else if($config instanceof Configuration)
        {
            $this->mergeRecursive($this->_configuration, $config->getArray());
        }
        else throw new \Exception("Can't merge unknown");
    }

    public function getArray()
    {
        return $this->_configuration;
    }


    public function get($name, $default = null)
    {
        $return = $default;
        
        if(\array_key_exists($name, $this->_configuration))
        {
            $return = $this->_configuration[$name];
        }
        return $return;
    }

    /**
     * Loaders
     */

    private function loadFromPHP($configPath)
    {
        return require $configPath;
    }

    private function loadFromXML()
    {}

    private function loadFromYAML($config)
    {
        return  Yaml::load($config);

        //die(\var_dump($loader));
        //1. Пропарсить конфиг
        //2. Найти элементы 'import' => 'конфиг'
        //3. Для каждой найденной пары заменить заменить на подгруженный
        // массив из нового конфига...
    }


    /**
     * Internal
     */

    private function mergeRecursive(&$target, $source)
    {
        if(\is_array($target) && \is_array($source))
        {
            foreach($source as $key => $value)
            {
                if(isset($target[$key]))
                {
                   $target[$key] = $this->mergeRecursive($target[$key], $value);
                }
                else
                {
                    if($key === 0)
                    {
                        $target = array(
                            0 => $this->mergeRecursive($target, $value),
                        );
                    }
                    else
                    {
                        $target[$key] = $value;
                    }
                }
            }
        }
        else
        {
            $target = $source;
        }
        
        return $target;
    }
    
    /**
     * Magic
     */

    public function  __isset($name) {
        return isset($this->_configuration[$name]);
        
    }
    
    public function  __get($name) {
        return $this->get($name, null);
        
    }
    
    public function  __set($name, $value) {
        ;
    }

    public function  __unset($name) {
        ;
    }

    /**
     * Interfaces
     */
    public function count() {

    }

    public function current() {

    }

    public function key() {

    }

    public function next() {

    }

    public function rewind() {

    }

    public function valid() {

    }
}
?>
